var models = require('../db');
var express = require('express');
var router = express.Router();
var mysql = require('mysql');
var $sql = require('../sqlMap');
// 连接数据库
var conn = mysql.createConnection(models.mysql);
conn.connect();
var jsonWrite = function(res, ret) {
	if (typeof ret === 'undefined') {
		res.json({
			code: '1',
			msg: '操作失败'
		});
	} else {
		res.json(
			ret
		);
	}
};

// 登录接口
router.post('/login', (req, res) => {
  var params = req.body;
  console.log(params, params.accounts)
  conn.query( /* "select accounts,password from dom01 where accounts = '"+params.accounts+"'and password ='"+params.password+"'" */
    "SELECT password FROM teacher WHERE accounts ='" + params.accounts + "'",
    function(err, result) {
      if (err) {
        console.log(err);
      }
      if (result) {
        jsonWrite(res, result);
      }
    })
})

// 配合登录查看登录的老师的接口
router.get('/query', (req, res) => {
	var params = req.query;
	conn.query('select * from teacher where accounts="' + params.accounts + '"', function(err, row) {
		if (err) {
			console.log(err)
		}
		let data = JSON.stringify(row);
		console.log(data)
		let newData = JSON.parse(data);
		if (newData.length === 0) {
			res.end(data);
		} else {
			res.end(data)
		}
		console.log(data)
		res.send(data)
	})
});

// 删库跑路
router.get('/delall', (req, res) => {
	var params = req.query;
	var paramsStr = JSON.stringify(params.tableName)
	var paramsJson = JSON.parse(paramsStr)
	console.log(paramsJson)
	conn.query('drop table ' + paramsJson +'', function(err, row) {
		if (err) {
			console.log(err)
		}
		let data = JSON.stringify(row);
		console.log(data)
		res.end('删除成功')
	})
});

// 增加教师用户接口
router.post('/adduser', (req, res) => {
	var sql = $sql.user.add;
	var params = req.body;
	console.log(params);
	conn.query(sql, [params.accounts, params.password, params.level, params.job,params.id], function(err, result) {
		if (err) {
			console.log(err);
		}
		if (result) {
			jsonWrite(res, result);
		}
	})
});


// 查看所有教师数据接口
router.get('/all', (req, res) => {
	conn.query('select * from teacher', function(err, row) {
		if (err) {
			console.log(err)
		}
		let data = JSON.stringify(row);
		res.end(data)
	})
});

// 删除多个教师的接口
router.get('/delmore', (req, res) => {
	var params = req.query.id;
	var paramsArr = params.split(',')
	console.log(paramsArr)
	for (let i = 0; i < paramsArr.length; i++) {
		conn.query('delete from teacher where id=' + paramsArr[i] + '', function(err, row) {
			if (err) {
				console.log(err)
			}
		})
	}
	res.end('没报错')
	// conn.query('delete')
})


// 删除单个教师帐号接口
router.get('/del', (req, res) => {
	var params = req.query;
	conn.query('delete from teacher where id="' + params.id + '"', function(err, row) {
		if (err) {
			console.log(err)
		}
		let data = JSON.stringify(row);
		res.end(data);
	})
})

// 修改单个教师的数据接口
router.post('/update', (req, res) => {
	var params = req.body;
	var paramsObj = JSON.stringify(params)
	var paramsJson = JSON.parse(paramsStr)
	for (let key in paramsJson) {
		conn.query('update teacher set ' + key + '="' + paramsJson[key] + '" where id="' + params.id + '"'),
			function(err, row) {
				if (err) {
					console.log(err)
				}
			}
	}
	res.end('修改成功')
}) 

// 增加学生接口
router.post('/addstudents', (req, res) => {
	var sql = $sql.user.addstudent;
	var params = req.body;
	console.log(params);
	conn.query(sql, [params.name, params.number, params.age, params.sex, params.class, params.phone, params
		.state, params.discipline
	], function(err, result) {
		if (err) {
			console.log(err);
		}
		if (result) {
			jsonWrite(res, result);
			res.end('success')
		}
	})
});

// 删除多个学生的接口
router.get('/delmorestudents', (req, res) => {
	var params = req.query.id;
	var paramsArr = params.split(',')
	console.log(paramsArr)
	for (let i = 0; i < paramsArr.length; i++) {
		conn.query('delete from students where id=' + paramsArr[i] + '', function(err, row) {
			if (err) {
				console.log(err)
			}
		})
	}
	res.end('没报错')
	// conn.query('delete')
})


// 查看所有学生数据接口
router.get('/allstudents', (req, res) => {
	conn.query('select * from students', function(err, row) {
		if (err) {
			console.log(err)
		}
		let data = JSON.stringify(row);
		res.end(data);
	})
});

// 修改单个学生的接口
router.post('/updatestudents', (req, res) => {
	var params = req.body;
	var paramsObj = JSON.stringify(params)
	var paramsJson = JSON.parse(paramsStr)
	for (let key in paramsJson) {
		conn.query('update students set ' + key + '="' + paramsJson[key] + '" where id="' + params.id + '"'),
			function(err, row) {
				if (err) {
					console.log(err)
				}
			}
	}
	res.end('修改成功')
}) 

// 删除单个学生数据接口
router.get('/delstudents', (req, res) => {
	var params = req.query;
	conn.query('delete from students where id="' + params.id + '"', function(err, row) {
		if (err) {
			console.log(err)
		}
		let data = JSON.stringify(row);
		res.end(data);
	})
})

module.exports = router;
